<template>
  <div class="page-box">
    <!-- 搜索区 -->
    <el-collapse-transition>
      <div v-show="showSearchBox" class="search-box">
        <!-- :inline="true" 表单在一行显示 -->
        <el-form :inline="true" class="demo-form-inline">
          <el-form-item label="标签名称">
            <el-input v-model.trim="queryParams.tagname" placeholder="请输入标签名称" />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="search">搜索</el-button>
            <el-button icon="el-icon-refresh" @click="reset">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-collapse-transition>
    <!-- 工具栏 -->
    <div class="toolbar-box">
      <el-row type="flex" justify="space-between">
        <div>
          <el-button type="primary" icon="el-icon-plus" plain @click="showDialog = true">新增</el-button>
          <el-button type="warning" icon="el-icon-download" plain>导出</el-button>
        </div>
        <div>
          <el-tooltip content="关闭搜索区" placement="top">
            <el-button icon="el-icon-arrow-up" circle @click="showSearchBox = !showSearchBox" />
          </el-tooltip>
          <el-tooltip content="刷新" placement="top">
            <el-button icon="el-icon-refresh" circle />
          </el-tooltip>
          <el-tooltip content="筛选" placement="top">
            <el-button icon="el-icon-menu" circle />
          </el-tooltip>
        </div>
      </el-row>
    </div>
    <!-- 表格 -->
    <div class="table-box">
      <el-table :data="list" style="width: 100%" border>
        <el-table-column type="index" label="#" width="50" />
        <el-table-column prop="id" label="ID" />
        <el-table-column prop="tagname" label="标签名称" />
        <el-table-column label="操作" align="center">
          <template #default="{row}">
            <el-button type="primary" circle icon="el-icon-edit" @click="editTag(row.id)" />
            <el-button type="danger" circle icon="el-icon-delete" @click="doDel(row.id)" />
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页器 -->
    <div class="pagination-box">
      <el-row type="flex" justify="end">
        <el-pagination
          background
          :current-page="queryParams.page"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="queryParams.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </el-row>
    </div>
    <!-- 新增和编辑弹出对话框 -->
    <el-dialog :title="dialogTitle" :visible.sync="showDialog" width="40%" @closed="close">
      <el-form ref="form" label-width="120px" :model="formData" :rules="rules">
        <el-form-item label="标签名称" prop="tagname">
          <el-input v-model="formData.tagname" style="width:400px" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showDialog = false">取消</el-button>
        <el-button type="primary" @click="submit">确认</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { addTag, delTag, getTagDetail, getTagList, updateTag } from '@/api/content/tag'
export default {
  data() {
    return {
      list: [],
      // 查询参数对象
      queryParams: {
        tagname: '',
        page: 1,
        limit: 10
      },
      total: 0,
      showSearchBox: true, // 控制是否显示搜索区
      showDialog: false, // 控制新增|编辑对话框的打开和关闭
      // 表单数据对象
      formData: {
        tagname: ''
      },
      rules: {
        tagname: [{ required: true, message: '请输入标签名称', trigger: 'blur' }]
      }
    }
  },
  computed: {
    // 处理对话框标题
    dialogTitle() {
      return this.formData.id ? '编辑标签' : '新增标签'
    }
  },
  created() {
    this.initData()
  },
  methods: {
    // 初始化分类列表
    async  initData() {
      const res = await getTagList(this.queryParams)
      this.list = res.data
      this.total = res.count
    },
    // 搜索
    search() {
      this.queryParams.page = 1
      this.initData()
    },
    // 重置搜索
    reset() {
      this.queryParams = {
        tagname: '',
        page: 1,
        limit: 10
      }
      this.initData()
    },
    // 切换每页显示数据条数
    handleSizeChange(value) {
      this.queryParams.page = 1
      this.queryParams.limit = value
      this.initData()
    },
    // 切换分页
    handleCurrentChange(value) {
      this.queryParams.page = value
      this.initData()
    },
    // 提交表单(编辑|添加)
    submit() {
      this.$refs.form.validate(async(isOk) => {
        if (!isOk) return
        await this.formData.id ? this.doEdit() : this.doAdd()
        this.showDialog = false
        // 重置表单数据
        // 只要关闭对话框就会自动触发close方法无需手动调用
        // this.close()
        // 刷新列表
        // this.queryParams.page = 1
        this.initData()
      })
    },
    // 关闭对话框后重置表单数据
    close() {
      this.formData = {
        tagname: ''
      }
      this.$refs.form.clearValidate()
    },
    async doAdd() {
      await addTag(this.formData)
      this.$message.success('新增标签成功！')
    },
    // 点击编辑按钮回显数据
    async editTag(id) {
      this.showDialog = true
      const res = await getTagDetail(id)
      this.formData = res
    },
    async doEdit() {
      await updateTag(this.formData)
      this.$message.success('修改标签成功！')
    },
    async doDel(id) {
      this.$confirm('确认删除该标签?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delTag(id)
        this.$message.success('删除成功！')
        // 当删除最后一页最后一条数据后回到上一页
        if (this.list.length === 1 && this.queryParams.page > 1) {
          this.queryParams.page--
        }
        this.initData()
      }).catch(() => {
        return
      })
    }
  }
}
</script>
<style lang="scss">

</style>

